#pragma once

#include <iostream>
#include <stdarg.h>
#include <time.h>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define SIZE 1024
#define INFO 0
#define DEBUG 1
#define WARNING 2
#define ERROR 3
#define FATAL 4

#define SCREEN 1
#define ONEFILE 2
#define CLASSIFY 3

#define LOGFILE "logtxt"
class Log
{
private:
    int _showMethod;
    std::string _path;

public:
    Log(int showMethod = SCREEN)
    :_showMethod(showMethod)
    {
        _path = "./log/";
    }

    ~Log()
    {}

    void Enable(int method)
    {
        _showMethod = method;
    }

    std::string levelToString(int level)
    {
        switch (level)
        {
        case INFO:
            return "info";
        case DEBUG:
            return "debug";
        case WARNING:
            return "warning";
        case ERROR:
            return "error";
        case FATAL:
            return "fatal";
        default:
            return "None";
        }
    }

    // void logmessgae(int level, char *format, ...)
    void logmessgae(int level, char *format, va_list& s)
    {
        // 默认
        time_t t = time(nullptr);
        struct tm *ctime = localtime(&t);
        char leftBuffer[SIZE];
        snprintf(leftBuffer, sizeof(leftBuffer), "[%s][%d-%d-%d, %d:%d:%d]", levelToString(level).c_str(), ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec);

        // 用户
        char rightBuffer[SIZE * 2];
        // va_list s;
        // va_start(s, format);
        vsnprintf(rightBuffer, sizeof(rightBuffer), format, s);
        // va_end(s);

        char logtxt[SIZE * 2];
        snprintf(logtxt, sizeof(logtxt), "%s %s", leftBuffer, rightBuffer);

        // printf("%s", logtxt);
        printLog(level, logtxt);

        // printf("%d-%d-%d, %d:%d:%d\n",ctime->tm_year+1900, ctime->tm_mon+1,ctime->tm_mday,ctime->tm_hour,ctime->tm_min,ctime->tm_sec); //年从1900开始，月从0开始

        // 格式：默认部分 + 自定义部分
    }

    void printLog(int level, const std::string& logtxt)
    {
        switch(_showMethod)
        {
            case SCREEN:
                std::cout<<logtxt<<std::endl; 
            break;
            case ONEFILE:
                PrintOnefile(LOGFILE,logtxt);             
            break;

            case CLASSIFY :      
                PrintClassfile(level, logtxt);             
            break;
            default: break;
        }
    }

    void PrintOnefile(const std::string& filename, const std::string& logtxt)
    {
        std::string logname = _path+filename;
        int fd = open(logname.c_str(),O_WRONLY|O_CREAT|O_APPEND,0666);
        std::cout<<logname<<std::endl;
        if(fd<0) return;
        write(fd,logtxt.c_str(),logtxt.size());
        close(fd);
    }

    void PrintClassfile(int level, const std::string& logtxt)
    {
        std::string filename = LOGFILE;
        filename += ".";
        filename += levelToString(level);
        PrintOnefile(filename,logtxt);
    }

    void operator ()(int level, char *format, ...)
    {
        va_list s;
        va_start(s, format);
        logmessgae(level,format,s);
        va_end(s);
        return;
    }
};
